import Toast from 'tdesign-miniprogram/toast/index';
import {
	fetchGood
} from '../../../services/good/fetchGood';
import {
	fetchActivityList
} from '../../../services/activity/fetchActivityList';
import {
	getGoodsDetailsCommentList,
	getGoodsDetailsCommentsCount,
} from '../../../services/good/fetchGoodsDetailsComments';

import {
	cdnBase
} from '../../../config/index';
const $api = require('../../../utils/request').Api;
const imgPrefix = `${cdnBase}/`;
const app = getApp()
const recLeftImg = `${imgPrefix}common/rec-left.png`;
const recRightImg = `${imgPrefix}common/rec-right.png`;
const obj2Params = (obj = {}, encode = false) => {
  const result = [];
  
	Object.keys(obj).forEach((key) =>
		result.push(`${key}=${encode ? encodeURIComponent(obj[key]) : obj[key]}`),
	);

	return result.join('&');
};

Page({
	data: {
    baseImgUrl: app.globalData.baseImgUrl,
		commentsList: [],
		commentsStatistics: {
			badCount: 0,
			commentCount: 0,
			goodCount: 0,
			goodRate: 0,
			hasImageCount: 0,
			middleCount: 0,
		},
		isShowPromotionPop: false,
		activityList: [],
		recLeftImg,
		recRightImg,
		details: {},
		goodsTabArray: [{
				name: '商品',
				value: '', // 空字符串代表置顶
			},
			{
				name: '详情',
				value: 'goods-page',
			},
		],
		storeLogo: `${imgPrefix}common/store-logo.png`,
		storeName: '云mall标准版旗舰店',
		jumpArray: [{
				title: '首页',
				url: '/pages/home/home',
				iconName: 'home',
			},
			{
				title: '购物车',
				url: '/pages/cart/index',
				iconName: 'cart',
				showCartNum: true,
			},
		],
		isStock: true,
		cartNum: 0,
		soldout: false,
		buttonType: 1,
		buyNum: 1,
		selectedAttrStr: '',
		skuArray: [],
		primaryImage: '',
		specImg: '',
		isSpuSelectPopupShow: false,
		isAllSelectedSku: false,
		buyType: 0,
		outOperateStatus: false, // 是否外层加入购物车
		operateType: 0,
		selectSkuSellsPrice: 0,
		maxLinePrice: 0,
		minSalePrice: 0,
		maxSalePrice: 0,
		list: [],
		spuId: '',
		navigation: {
			type: 'fraction'
		},
		current: 0,
		autoplay: true,
		duration: 500,
		interval: 5000,
		soldNum: 0, // 已售数量
		list: [],
		modifiedData: []
	},

	handlePopupHide() {
		this.setData({
			isSpuSelectPopupShow: false,
		});
	},

	showSkuSelectPopup(type) {
		this.setData({
			buyType: type || 0,
			outOperateStatus: type >= 1,
			isSpuSelectPopupShow: true,
		});
	},

	buyItNow() {
		this.showSkuSelectPopup(1);
	},

	toAddCart() {
		this.showSkuSelectPopup(2);
	},

	toNav(e) {
		const {
			url
		} = e.detail;
		wx.switchTab({
			url: url,
		});
	},

	showCurImg(e) {
		const {
			index
		} = e.detail;
		const {
			images
		} = this.data.details;
		wx.previewImage({
			current: images[index],
			urls: images, // 需要预览的图片http链接列表
		});
	},

	onPageScroll({
		scrollTop
	}) {
		const goodsTab = this.selectComponent('#goodsTab');
		goodsTab && goodsTab.onScroll(scrollTop);
	},

	chooseSpecItem(e) {
		console.log(e);
		const {
			specInfo
		} = this.data.skuArray[0];
		const {
			selectedSku,
			isAllSelectedSku
		} = e.detail;
		if (!isAllSelectedSku) {
			this.setData({
				selectSkuSellsPrice: 0,
			});
		}
		this.setData({
			isAllSelectedSku,
		});
		console.log(specInfo);
		this.getSkuItem(specInfo, selectedSku);
	},

	getSkuItem(specList, selectedSku) {
		const {
			skuArray,
			primaryImage
		} = this.data;
		console.log(specList);
		const selectedSkuValues = this.getSelectedSkuValues(specList, selectedSku);
		let selectedAttrStr = ` ${this.data.buyNum}件 `;
		selectedSkuValues.forEach((item) => {
			console.log(item);
			selectedAttrStr += `，${item.attr_name}  `;
		});
		// eslint-disable-next-line array-callback-return
		const skuItem = skuArray.filter((item) => {
			let status = true;
			(item.specInfo || []).forEach((subItem) => {
				if (
					!selectedSku[subItem.specId] ||
					selectedSku[subItem.specId] !== subItem.specValueId
				) {
					status = false;
				}
			});
			if (status) return item;
		});
		this.selectSpecsName(selectedSkuValues.length > 0 ? selectedAttrStr : '');
		if (skuItem) {
			console.log(skuItem);
			this.setData({
				selectItem: skuItem,
				selectSkuSellsPrice: skuItem.price || 0,
			});
		} else {
			this.setData({
				selectItem: null,
				selectSkuSellsPrice: 0,
			});
		}
		this.setData({
			specImg: skuItem && skuItem.skuImage ? skuItem.skuImage : primaryImage,
		});
	},

	// 获取已选择的sku名称
	getSelectedSkuValues(skuTree, selectedSku) {
		console.log(skuTree, selectedSku);
		const normalizedTree = this.normalizeSkuTree(skuTree);
		return Object.keys(selectedSku).reduce((selectedValues, skuKeyStr) => {
			console.log(normalizedTree, skuKeyStr);
			const skuValues = normalizedTree[skuKeyStr];
			const skuValueId = selectedSku[skuKeyStr];
			if (skuValueId !== '') {
				const skuValue = skuValues.filter((value) => {
					console.log(value);
					return value.attr_id === skuValueId;
				})[0];
				skuValue && selectedValues.push(skuValue);
			}
			console.log(selectedValues);
			const add = []
			selectedValues.forEach((item, index) => {
				const list = {
					"attrName": item.attr_name
				}
				add.push(list)
			})
			console.log(JSON.stringify(add));

			wx.request({
				url: app.globalData.baseApiUrl +'/api/goods/getGoodsInventory',
				method: 'POST',
				data: {
					productId: 1503,
					productSize: JSON.stringify(add),
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
				},
				success: res => {
					// do something
					console.log(res);
					res.data.data.forEach((item, index) => {
						let arr = JSON.parse(item.inventoryList)
						console.log(arr);
						this.setData({
							modifiedData: arr
						})
						// this.data.skuArray.forEach((item, index) => {
						// 	console.log(item);
						// 	item.specInfo.forEach((xitm, index) => {
						// 		let modifiedData = xitm.attr_list.map((iop, index) => {
						// 			console.log(iop);
						// 			console.log(arr, iop.attr_name);
						// 			if (arr[0].attrName === iop.attr_name) {

						// 				if (arr[0].inventory > 0) {
						// 					iop.status = false
						// 				} else {
						// 					iop.status = true
						// 				}
						// 			}
						// 			return iop
						// 		})
						// 		// console.log(modifiedData);

						// 	})

						// })

					})

				},
				fail: err => {}
			})
			// $api.getGoodsInventory({
			// 	productId: 1503,
			// 	productSize: add,
			// }).then((res) => {
			// 	console.log(res);
			// })
			return selectedValues;
		}, []);
	},

	normalizeSkuTree(skuTree) {
		console.log(skuTree);
		const normalizedTree = {};
		skuTree.forEach((treeItem) => {
			normalizedTree[treeItem.attr_group_id] = treeItem.attr_list;
		});
		return normalizedTree;
	},

	selectSpecsName(selectSpecsName) {
		if (selectSpecsName) {
			this.setData({
				selectedAttrStr: selectSpecsName,
			});
		} else {
			this.setData({
				selectedAttrStr: '',
			});
		}
	},

	addCart() {
		const {
			isAllSelectedSku
		} = this.data;
		Toast({
			context: this,
			selector: '#t-toast',
			message: isAllSelectedSku ? '点击加入购物车' : '请选择规格',
			icon: '',
			duration: 1000,
		});
	},

	gotoBuy(type) {
		console.log(type);
		const {
			isAllSelectedSku,
			buyNum
		} = this.data;
		if (!isAllSelectedSku) {
			Toast({
				context: this,
				selector: '#t-toast',
				message: '请选择规格',
				icon: '',
				duration: 1000,
			});
			return;
		}
		this.handlePopupHide();
		const query = {
			selectedAttrStr: this.data.selectedAttrStr,
			quantity: this.data.buyNum,
			storeId: '1',
			spuId: this.data.spuId,
			goodsName: this.data.details.title,
			skuId: type === 1 ? this.data.skuList[0].skuId : this.data.selectItem.skuId,
			available: this.data.details.available,
			price: this.data.details.yprice,
			specInfo: this.data.selectItem?.map((item) => ({
				specTitle: item.specInfo[0].specValue,
				specValue: item.specInfo[1].specValue,
			})),
			primaryImage: this.data.details.primaryImage,
			spuId: this.data.details.spuId,
			thumb: this.data.details.primaryImage,
			title: this.data.details.productTitle,
		};
		let urlQueryStr = obj2Params({
			goodsRequestList: JSON.stringify([query]),
		});
		urlQueryStr = urlQueryStr ? `?${urlQueryStr}` : '';
		// console.log(urlQueryStr);
		const list = JSON.stringify(this.data.details[0])

		const path = `/pages/order/order-confirm/index${urlQueryStr}&list=${list}&buyNum=${this.data.buyNum}&selectedAttrStr=${this.data.selectedAttrStr}`;
		wx.navigateTo({
			url: path,
		});
	},

	specsConfirm(list) {
		let userInfo = wx.getStorageSync('userInfo')
		if (userInfo) {
			const {
				buyType
			} = this.data;
			if (buyType === 1) {
				this.gotoBuy(list);
			} else {
				this.addCart(list);
			}
		} else {
			wx.showToast({ // 显示加载中loading效果 
				title: "请您先登录",
				icon: 'error',
				duration: 2000,
				success: function () {
					// console.log('sssssssssssssssssssssssssssssssss');
					setTimeout(() => {
						wx.navigateTo({
							url: '/pages/usercenter/userAuthorized/Authorized'
						})
					}, 2000)
				}
			});
		}

		// this.handlePopupHide();
	},

	changeNum(e) {
		this.setData({
			buyNum: e.detail.buyNum,
		});
	},

	closePromotionPopup() {
		this.setData({
			isShowPromotionPop: false,
		});
	},

	promotionChange(e) {
		const {
			index
		} = e.detail;
		wx.navigateTo({
			url: `/pages/promotion-detail/index?promotion_id=${index}`,
		});
	},

	showPromotionPopup() {
		this.setData({
			isShowPromotionPop: true,
		});
	},

	getDetail(spuId) {
		$api.goodsDetails({
			productNumber: spuId
		}).then((res) => {
			// console.log(spuId);
			// console.log(res);
			const details = res.data.data;
			console.log(details);
			// console.log(details);
			const skuArray = [];
			const skuList = []
			$api.getGoodsSku({
				// id:spuId
				id: 1503
			}).then((res) => {
				console.log(res);
				// const {
				// 	skuList,
				// 	primaryImage,
				// 	isPutOnSale,
				// 	minSalePrice,
				// 	maxSalePrice,
				// 	maxLinePrice,
				// 	soldNum,
				// } = details;
				console.log(res.data.data);
				skuArray.push({
					skuId: res.data.data.attr_group_list[0].attr_group_id,
					specInfo: res.data.data.attr_group_list,
				});
				this.setData({
					details,
					maxSalePrice: details[0].yprice,
					maxLinePrice: details[0].price,
					minSalePrice: details[0].yprice,
					// activityList,
					isStock: true,
					// maxSalePrice: maxSalePrice ? parseInt(maxSalePrice) : 0,
					// maxLinePrice: maxLinePrice ? parseInt(maxLinePrice) : 0,
					// minSalePrice: minSalePrice ? parseInt(minSalePrice) : 0,
					// list: promotionArray,
					skuArray: skuArray,
					// primaryImage,
					// soldout: isPutOnSale === 0,
					// soldNum,
				});
			})
		})


		// Promise.all([fetchGood(spuId), fetchActivityList()]).then((res) => {
		// 	const [details, activityList] = res;
		// 	const skuArray = [];
		// 	const {
		// 		skuList,
		// 		primaryImage,
		// 		isPutOnSale,
		// 		minSalePrice,
		// 		maxSalePrice,
		// 		maxLinePrice,
		// 		soldNum,
		// 	} = details;
		// 	skuList.forEach((item) => {
		// 		skuArray.push({
		// 			skuId: item.skuId,
		// 			quantity: item.stockInfo ? item.stockInfo.stockQuantity : 0,
		// 			specInfo: item.specInfo,
		// 		});
		// 	});
		// 	const promotionArray = [];
		// 	activityList.forEach((item) => {
		// 		promotionArray.push({
		// 			tag: item.promotionSubCode === 'MYJ' ? '满减' : '满折',
		// 			label: '满100元减99.9元',
		// 		});
		// 	});
		// 	this.setData({
		// 		details,
		// 		activityList,
		// 		isStock: details.spuStockQuantity > 0,
		// 		maxSalePrice: maxSalePrice ? parseInt(maxSalePrice) : 0,
		// 		maxLinePrice: maxLinePrice ? parseInt(maxLinePrice) : 0,
		// 		minSalePrice: minSalePrice ? parseInt(minSalePrice) : 0,
		// 		list: promotionArray,
		// 		skuArray: skuArray,
		// 		primaryImage,
		// 		soldout: isPutOnSale === 0,
		// 		soldNum,
		// 	});
		// });
	},

	async getCommentsList() {
		try {
			const code = 'Success';
			const data = await getGoodsDetailsCommentList();
			const {
				homePageComments
			} = data;
			if (code.toUpperCase() === 'SUCCESS') {
				const nextState = {
					commentsList: homePageComments.map((item) => {
						return {
							goodsSpu: item.spuId,
							userName: item.userName || '',
							commentScore: item.commentScore,
							commentContent: item.commentContent || '用户未填写评价',
							userHeadUrl: item.isAnonymity ?
								this.anonymityAvatar : item.userHeadUrl || this.anonymityAvatar,
						};
					}),
				};
				this.setData(nextState);
			}
		} catch (error) {
			console.error('comments error:', error);
		}
	},

	// onShareAppMessage() {
	// 	// 自定义的返回信息
	// 	const {
	// 		selectedAttrStr
	// 	} = this.data;
	// 	let shareSubTitle = '';
	// 	if (selectedAttrStr.indexOf('件') > -1) {
	// 		const count = selectedAttrStr.indexOf('件');
	// 		shareSubTitle = selectedAttrStr.slice(count + 1, selectedAttrStr.length);
	// 	}
	// 	const customInfo = {
	// 		imageUrl: this.data.details.primaryImage,
	// 		title: this.data.details.title + shareSubTitle,
	// 		path: `/pages/goods/details/index?spuId=${this.data.spuId}`,
	// 	};
	// 	return customInfo;
	// },

	/** 获取评价统计 */
	async getCommentsStatistics() {
		try {
			const code = 'Success';
			const data = await getGoodsDetailsCommentsCount();
			if (code.toUpperCase() === 'SUCCESS') {
				const {
					badCount,
					commentCount,
					goodCount,
					goodRate,
					hasImageCount,
					middleCount,
				} = data;
				const nextState = {
					commentsStatistics: {
						badCount: parseInt(`${badCount}`),
						commentCount: parseInt(`${commentCount}`),
						goodCount: parseInt(`${goodCount}`),
						/** 后端返回百分比后数据但没有限制位数 */
						goodRate: Math.floor(goodRate * 10) / 10,
						hasImageCount: parseInt(`${hasImageCount}`),
						middleCount: parseInt(`${middleCount}`),
					},
				};
				this.setData(nextState);
			}
		} catch (error) {
			console.error('comments statiistics error:', error);
		}
	},

	/** 跳转到评价列表 */
	navToCommentsListPage() {
		wx.navigateTo({
			url: `/pages/goods/comments/index?spuId=${this.data.spuId}`,
		});
	},

	onLoad(query) {
		const {
			spuId
		} = query;
		this.setData({
			spuId: spuId,
		});
		this.getDetail(spuId);
		this.getCommentsList(spuId);
		this.getCommentsStatistics(spuId);
	},
});